package com.jtjrenren.android.taxi.passenger.core.web;

import android.content.Context;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;

/**
 * Created by Administrator on 2016/1/23.
 */
public class AppShowHtmlActivity extends BaseActivity {
    public static final String EXTRAS_URL = "url";
    public static final String EXTRAS_TITLE = "title";
    public static final String EXTRAS_TITLE_SHOW = "title_show";
    private WebView wbView;
    private TextView tv_tilte;
    private RelativeLayout rl_title;

    private String title, url;
    private boolean showTitle = true;


    @Override
    public int getLayoutId() {
        return R.layout.aty_html_show;
    }

    @Override
    public void init() {
        initData();

        initViews();

        webViewConfig();

        wbView.setWebChromeClient(new WebChromeClient());

        wbView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");

        loadUrl();
    }

    /**
     * webview配置
     */
    private void webViewConfig() {
        WebSettings setting = wbView.getSettings();
        setting.setJavaScriptEnabled(true);//添加js支持
        setting.setAllowFileAccess(true);
        setting.setDomStorageEnabled(true);
//        setting.setBlockNetworkImage(false);
//        setting.setBlockNetworkLoads(false);
//        setting.setSaveFormData(false);
//        setting.setSupportZoom(false);
        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);

        setting.setDatabaseEnabled(true);
        String dir = this.getApplicationContext().getDir("database",
                Context.MODE_PRIVATE).getPath();
        setting.setGeolocationEnabled(true);
        setting.setGeolocationDatabasePath(dir);
    }

    /**
     * 加载url
     */
    private void loadUrl() {
        showWaittingDialog(null, false);

        wbView.loadUrl(url);
        // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        wbView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissWaittingDialog();
            }


        });
    }

    private void initViews() {
        wbView = (WebView) findViewById(R.id.html_show_web);
        tv_tilte = (TextView) findViewById(R.id.title);
        rl_title = (RelativeLayout) findViewById(R.id.title_back);


        findViewById(R.id.title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if (!showTitle) {
            rl_title.setVisibility(View.GONE);
        } else if (title != null)
            tv_tilte.setText(title);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        if (getIntent().hasExtra(EXTRAS_TITLE)) {
            title = getIntent().getStringExtra(EXTRAS_TITLE);
        }
        if (getIntent().hasExtra(EXTRAS_URL)) {
            url = getIntent().getStringExtra(EXTRAS_URL);
        }
        if (getIntent().hasExtra(EXTRAS_TITLE_SHOW)) {
            showTitle = getIntent().getBooleanExtra(EXTRAS_TITLE_SHOW, true);
        }
    }

    final class DemoJavaScriptInterface {

        DemoJavaScriptInterface() {
        }

        /**
         * This is not called on the UI thread. Post a runnable to invoke
         * loadUrl on the UI thread.
         */
        @JavascriptInterface
        public void clickOnAndroid() {
            finish();
        }
    }

}
